package com.ez.supervise.action.biz;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.model.base.BaseEntity;
import com.ez.supervise.model.biz.DemocracyGoal;
import com.ez.supervise.model.biz.DemocracyScore;
import com.ez.supervise.model.biz.File;
import com.ez.supervise.model.biz.OrgDemocracy;
import com.ez.supervise.model.dto.LoginUser;
import com.ez.supervise.model.dto.QueryDemocracyDto;
import com.ez.supervise.model.sys.Org;
import com.ez.supervise.service.DemocracyGoalService;
import com.ez.supervise.service.DemocracyScoreService;
import com.ez.supervise.service.OrgDemocracyService;
import com.ez.supervise.service.OrgService;
import com.ez.supervise.util.ConvertUtil;
import com.ez.supervise.util.Format;
import com.ez.supervise.util.FreemarkerUtil;

public class QueryDemocracyAction extends BaseJsonAction {

    private static final long serialVersionUID = -5237037468195860016L;
    private Long id;
    private OrgDemocracyService orgDemocracyService;
    private DemocracyGoalService democracyGoalService;
    private DemocracyScoreService democracyScoreService;

    private OrgDemocracy orgDemocracy;
    private OrgService orgService;

    private Page page;
    private Integer year;
    private String orgType;
    private String queryType;
    private Long checkManId;
    private List<Org> orgList;
    private List<DemocracyGoal> democracyGoalList;
    private static List<DemocracyScore> democracyScoreList;
    private Map<String, String> democracyMap;

    public String queryDemocracy() throws Exception {
	orgList = orgService.findOrgByType(orgType);
	if (year == null) {
	    year = Calendar.getInstance().get(Calendar.YEAR);
	}
	democracyGoalList = democracyGoalService.findDemocracyGoal(year, orgType);
	if ("point".equals(queryType)) {
	    List democracyList = orgDemocracyService.queryDemocracy(year, orgType, queryType, checkManId);
	    democracyMap = convertMap((List<QueryDemocracyDto>) democracyList, queryType);
	    return "point";
	} else {
	    List democracyList = orgDemocracyService.queryDemocracy(year, orgType, queryType, checkManId);
	    democracyMap = convertMap((List<QueryDemocracyDto>) democracyList, queryType);
	    return SUCCESS;
	}
    }

    public String reportDemocracy() throws Exception {
	// boolean login = this.checkLogin();
	// if(!login){
	// return SUCCESS;
	// }
	orgList = orgService.findAll();
	if (year == null) {
	    year = Calendar.getInstance().get(Calendar.YEAR);
	}
	democracyGoalList = democracyGoalService.findDemocracyGoal(year, null);
	List democracyList = orgDemocracyService.queryDemocracy(year, null, queryType, null);
	democracyMap = convertMap((List<QueryDemocracyDto>) democracyList, queryType);
	try {
	    Map result = new HashMap();
	    result.put("year", year);
	    result.put("orgList", orgList);
	    result.put("democracyScoreList", getDemocracyScoreList());
	    result.put("democracyGoalList", democracyGoalList);
	    result.put("democracyList", democracyList);
	    result.put("democracyMap", democracyMap);

	    boolean flag = createHtml(result);
	    if (!flag) {
		outJsonString(RESULT_FAIL);
		return SUCCESS;
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    e.printStackTrace();
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    private boolean createHtml(Map result) {
	boolean flag = FreemarkerUtil.createHtml("index-democracy.ftl", result, WEB_SITE_PATH, "index-democracy.htm");
	return flag;
    }

    /**
     * @param democracyList
     * @return
     */
    private Map<String, String> convertMap(List<QueryDemocracyDto> democracyList, String queryType) {
	if (democracyList == null) {
	    return null;
	} else {
	    Map<String, String> resultMap = new HashMap<String, String>();
	    for (QueryDemocracyDto dto : democracyList) {
		if (dto != null) {
//		    if("point".equals(queryType)){
//			 resultMap.put(dto.getOrgId() + "_" + dto.getScoreId(), Format.format(dto.getScore(), queryType));
//		    } else {
			 resultMap.put(dto.getOrgId() + "_" + dto.getScoreId(), Format.format(dto.getScore(), queryType));
//		    }
		}
	    }
	    return resultMap;
	}
    }

    public void setYear(Integer year) {
	this.year = year;
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public void setOrgType(String orgType) {
	this.orgType = orgType;
    }

    public String getOrgType() {
	return orgType;
    }

    public void setOrgDemocracyService(OrgDemocracyService orgDemocracyService) {
	this.orgDemocracyService = orgDemocracyService;
    }

    public void setOrgService(OrgService orgService) {
	this.orgService = orgService;
    }

    public void setDemocracyGoalService(DemocracyGoalService democracyGoalService) {
	this.democracyGoalService = democracyGoalService;
    }

    public void setDemocracyScoreService(DemocracyScoreService democracyScoreService) {
	this.democracyScoreService = democracyScoreService;
    }

    public OrgDemocracy getOrgDemocracy() {
	return orgDemocracy;
    }

    public void setOrgDemocracy(OrgDemocracy orgDemocracy) {
	this.orgDemocracy = orgDemocracy;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

    public void setOrgList(List<Org> orgList) {
	this.orgList = orgList;
    }

    public List<Org> getOrgList() {
	return orgList;
    }

    public List<DemocracyScore> getDemocracyScoreList() {
	if (democracyScoreList == null) {
	    democracyScoreList = democracyScoreService.findAll();
	}
	return democracyScoreList;
    }

    public static void clearDemocracyScoreList() {
	if (democracyScoreList != null) {
	    democracyScoreList.clear();
	    democracyScoreList = null;
	}
    }

    public void setDemocracyGoalList(List<DemocracyGoal> democracyGoalList) {
	this.democracyGoalList = democracyGoalList;
    }

    public List<DemocracyGoal> getDemocracyGoalList() {
	return democracyGoalList;
    }

    public String calcSorce(Long orgId, Long scoreId) {
	if (democracyMap == null) {
	    if ("point".equals(queryType)) {
		return "0.00";
	    } else {
		return "0";
	    }
	} else {
	    String reult = democracyMap.get(orgId + "_" + scoreId);
	    if (reult == null) {
		if ("point".equals(queryType)) {
		    return "0.00";
		} else {
		    return "0";
		}
	    } else {
		return reult;
	    }
	}
    }

    public static void main(String[] args) {

    }

    public final static List<OrgDemocracy> makeDemocracy(String data, Integer year, LoginUser loginUser) {
	String temp = "";
	String[] tempArray;
	List<OrgDemocracy> dataList = new ArrayList<OrgDemocracy>();
	OrgDemocracy orgDemocracy = null;
	String[] arrayData = data.split(",");
	if (ArrayUtils.isEmpty(arrayData)) {
	    return dataList;
	}
	for (int i = 0; i < arrayData.length; i++) {
	    temp = arrayData[i];
	    tempArray = temp.split("=");
	    temp = tempArray[0];
	    orgDemocracy = new OrgDemocracy();
	    orgDemocracy.setScoreId(Long.valueOf(tempArray[1]));
	    tempArray = temp.split("_");
	    orgDemocracy.setOrgId(Long.valueOf(tempArray[2]));
	    orgDemocracy.setGoalId(Long.valueOf(tempArray[3]));
	    orgDemocracy.setYear(year);
	    orgDemocracy.setCheckManId(loginUser.getDemocracyId());
	    orgDemocracy.setUserId(loginUser.getUserId());
	    dataList.add(orgDemocracy);
	}
	return dataList;
    }

    public void setQueryType(String queryType) {
	this.queryType = queryType;
    }

    public String getQueryType() {
	return queryType;
    }

    public void setCheckManId(Long checkManId) {
	this.checkManId = checkManId;
    }

    public Long getCheckManId() {
	return checkManId;
    }

}
